import Vue from 'vue'
import moment from 'moment'
import 'moment/locale/zh-cn'
moment.locale('zh-cn')

export function NumberFormat(value) {
  if (!value) {
    return '0'
  }
  const intPartFormat = value.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') // 将整数部分逢三一断
  return intPartFormat
}
export function dateFormat(dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
  return moment(dataStr).format(pattern)
}

export function textWidth(text){
   const canvas = document.createElement('canvas')
   const ctx = canvas.getContext('2d')
   let width = ctx.measureText(text).width
   return width * 1.44
}

Vue.filter('NumberFormat', NumberFormat)
Vue.filter('moment', dateFormat)

Vue.prototype.$filters = {
  NumberFormat,
  dateFormat,
  textWidth
}
